const startGameBtn = document.getElementById('start-game-btn');

const ROCK ='rock';
const PAPER ='paper';
const SCISSORS ='scissors';
const DEFAULT_USER_CHOICE=ROCK;
const RESULT_DRAW = 'DRAW';
const RESULT_PLAYER_WINS='PLAYER_WINS';
const RESULT_COMPUTER_WINS='COMPUTER_WINS';


let gameIsRunning = false;
const getPlayerChoice=function(){
    const selection = prompt(`${ROCK},${PAPER} OR ${SCISSORS}`,'').toUpperCase();
    if(selection !== ROCK &&
    selection !== PAPER &&
    selection !== SCISSORS){
        alert(`Invalid choice! We chose ${DEFAULT_USER_CHOICE} for you!`);
        return DEFAULT_USER_CHOICE;
    }
    return selection;
};

const getComputerChoice=function(){
    const randomValue = Math.random();
    if(randomValue < 0.34){
        return ROCK;
    }else if(randomValue < 0.67){
        return PAPER;
    }else{
        return SCISSORS;
    }
}

const getWinner = (cChoice,pChoice = DEFAULT_USER_CHOICE)=>
     cChoice === pChoice
        ? RESULT_DRAW
        :(cChoice === ROCK && pChoice === PAPER ||
         cChoice === PAPER && pChoice === SCISSORS ||
         cChoice === SCISSORS && pChoice === ROCK)
        ? RESULT_PLAYER_WINS : RESULT_COMPUTER_WINS;

startGameBtn.addEventListener('click', function(){
    if(gameIsRunning){
        return;
    }
    gameIsRunning = true;
    console.log('Game is starting...');
    const playerChoice = getPlayerChoice();
    const computerChoice = getComputerChoice();
    let winner;
    if(playerChoice){
        winner = getWinner(computerChoice,playerChoice);
    }else{
        winner = getWinner(computerChoice);
    }
    console.log(winner);
    let message = `You picked ${playerChoice || DEFAULT_USER_CHOICE},computer picked ${computerChoice},then you `;
    if(winner === RESULT_DRAW){
        message += 'had a draw.';
    }else if(winner === RESULT_PLAYER_WINS){
        message += 'won.';
    }else{
        message+='lost.';
    }
    alert(message);
    gameIsRunning = false;
});


